<?php
require("config/config.php");
require "common.php";
$mysql = new SaeMysql();
 
$uid= strip_tags( $_REQUEST['uid'] );
$nake = strip_tags( $_REQUEST['nake'] );

if(is_numeric($uid) && isset($nake))
{
	$sql = "SELECT * FROM `user` WHERE uid=".$uid;
	$result = $mysql->getData( $sql );
	$num = count($result);

	if($num == 1 && $result!=""){
		$rs=$result[0];
		$grade = $rs[grade];
		$sqlnake = $rs[nake];
		$sqldate = $rs[time];
		$date = date('Y-m-j');
          
		//grade+1
		$d = (maketime($date) - maketime($sqldate)) / (3600*24);
		if($d > 0){
			$grade += 1;
			$sql = "UPDATE `user` SET grade=".$grade." WHERE uid=".$uid;
			$mysql->runSql( $sql );
			
			$sql = "UPDATE `user` SET time=NOW() WHERE uid=".$uid;
			$mysql->runSql( $sql );
		}	  
		  //update name
		if($sqlnake != $nake){
			$sql = "UPDATE `user` SET nake='".$mysql->escape($nake)."' WHERE uid=".$uid;
			$mysql->runSql( $sql );
		}	  
	}else{
		$sql = "INSERT  INTO `user` ( `uid` , `nake`, `time` , `grade`, `money`) VALUES ( '".$mysql->escape($uid). "' , '"  . $mysql->escape($nake) . "' , NOW(), '0' , '0') ";
		$mysql->runSql( $sql );

		$sql = "SELECT * FROM `user` WHERE uid=".$uid;
		$result = $mysql->getData( $sql );
	}

	if( $mysql->errno() != 0 )
	{
		echo "false";
	}else{
		echo myjson($result);
	}
}else{
	echo "false";
}

$mysql->closeDb();
?>